﻿using System;
using System.Diagnostics;
using System.Windows;
using HelloWindowsPhoneMVC.Model;
using HelloWindowsPhoneMVC.Services;
using HelloWindowsPhoneMVC.ViewModels.Another;
using WindowsPhoneEssentials.Threading;
using WindowsPhoneMVC;
using WindowsPhoneMVC.ActionResults;

namespace HelloWindowsPhoneMVC.Controllers
{
    public class AnotherController : Controller
    {
        private readonly Func<IA> reflectionRoot;
        private readonly Func<IA2> explcitRoot;

        public AnotherController(Func<IA> reflectionRoot, Func<IA2> explcitRoot)
        {
            this.reflectionRoot = reflectionRoot;
            this.explcitRoot = explcitRoot;
        }

        public ActionResult BackTest()
        {
            return Back();
        }

        public ActionResult Example()
        {
            return Page(new ExampleViewModel());
        }

        public ActionResult ItemAction(ItemFromList item)
        {
            Execute.OnUIThreadSync(() => MessageBox.Show("Clicked on " + item.Title));

            return Nothing();
        }

        public ActionResult BenchmarkIoC()
        {
            var stopwatch = new Stopwatch();
            var stopwatch2 = new Stopwatch();

            stopwatch.Start();
            //Resolves 90 objects using autofac
            for (var i = 0; i < 10; i++)
            {
                var a = reflectionRoot();
            }
            stopwatch.Stop();

            stopwatch2.Start();
            //Resolves 90 objects using autofac
            for (int i = 0; i < 10; i++)
            {
                var a = explcitRoot();
            }
            stopwatch2.Stop();

            Execute.OnUIThreadSync(() =>
                                       {
                                           MessageBox.Show(string.Format("Self construction: {0} ms\r\nReflection: {1} ms",
                                                                         stopwatch2.ElapsedMilliseconds,
                                                                         stopwatch.ElapsedMilliseconds), "Results", MessageBoxButton.OK);
                                       });

            return Nothing();
        }

        public ActionResult ComplexArg(SomeClass parameterWritable)
        {
            return Page(parameterWritable);
        }

        public ActionResult Save()
        {
            return BackTo<HomeController>("Main");
        }

        public ActionResult PerformSearch(SomeClass parameter)
        {
            return Result(parameter.Message);
        }
    }
}